//
//  UIView+MSPlacehodlerView.m
//  PeopleForTheFirstMall
//
//  Created by iOS on 2019/8/9.
//  Copyright © 2019 MS. All rights reserved.
//

#import "UIView+MSPlacehodlerView.h"
#import <objc/runtime.h>

@interface UIView ()

@property (nonatomic, strong)   MSPlacehodlerView               *placeholderView;   ///< 占位图
@property (nonatomic)           UITableViewCellSeparatorStyle   tableViewSepStyle;  ///< 如果存在 tableview 时 ，处理

@end

@implementation UIView (MSPlacehodlerView)

- (void)showPlaceholderViewWithStatus:(MSPlacehodlerViewStatus)status
           description:(nonnull NSString *)description
{
    [self showPlaceholderViewWithStatus:status
                                  frame:CGRectZero
                            description:description];
}

- (void)showPlaceholderViewWithStatus:(MSPlacehodlerViewStatus)status
                                frame:(CGRect)frame
                          description:(nonnull NSString *)description
{
    if (!self.placeholderView)
    {
        if ([self isKindOfClass:[UIScrollView class]])
        {
            [(UIScrollView *)self setScrollEnabled:NO];
            
            if ([self isKindOfClass:[UITableView class]])
            {
                self.tableViewSepStyle = [(UITableView *)self separatorStyle];
                [(UITableView *)self setSeparatorStyle:UITableViewCellSeparatorStyleNone];
            }
        }
        
        MSPlacehodlerView *placeholderView = [[MSPlacehodlerView alloc] init];
        
        switch (status)
        {
            case MSPlacehodlerViewStatusNoNetwork:
                placeholderView.placeholderImg = @"placeholderView_failed_noNetwork";
                break;
            case MSPlacehodlerViewStatusNoData:
                placeholderView.placeholderImg = @"placeholderView_noData";
                break;
            case MSPlacehodlerViewStatus404:
                placeholderView.placeholderImg = @"placeholderView_404";
                break;
            case MSPlacehodlerViewStatusInDevelopment:
                placeholderView.placeholderImg = @"placeholderView_inDevelopment";
                break;
            default:
                break;
        }
        
        placeholderView.text = description;
        
        [self addSubview:placeholderView];
        
        if (CGRectEqualToRect(frame, CGRectZero))
        {
            [placeholderView mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.left.equalTo(self);
                make.size.equalTo(self);
            }];
        }
        else
        {
            [placeholderView mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.top.mas_equalTo(frame.origin.y);
                make.left.mas_equalTo(frame.origin.x);
                make.size.mas_equalTo(frame.size);
            }];
        }
        
        self.placeholderView = placeholderView;
    }
    else if (!CGRectEqualToRect(frame, CGRectZero))
    {
        [self.placeholderView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(frame.origin.y);
            make.left.mas_equalTo(frame.origin.x);
            make.size.mas_equalTo(frame.size);
        }];
    }
}

- (void)hiddenPlaceholderView
{
    if (self.placeholderView)
    {
        [self.placeholderView removeFromSuperview];
        
        if ([self isKindOfClass:[UIScrollView class]])
        {
            [(UIScrollView *)self setScrollEnabled:YES];
            
            if ([self isKindOfClass:[UITableView class]])
            {
                [(UITableView *)self setSeparatorStyle:self.tableViewSepStyle];
            }
        }
        
        self.placeholderView = nil;
    }
}

- (void)setPlaceholderView:(MSPlacehodlerView *)placeholderView
{
    objc_setAssociatedObject(self, @selector(placeholderView), placeholderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (MSPlacehodlerView *)placeholderView
{
    return objc_getAssociatedObject(self, _cmd);
}

-(void)setTableViewSepStyle:(UITableViewCellSeparatorStyle)tableViewSepStyle
{
    objc_setAssociatedObject(self, @selector(tableViewSepStyle), @(tableViewSepStyle), OBJC_ASSOCIATION_ASSIGN);
}
-(UITableViewCellSeparatorStyle)tableViewSepStyle
{
    NSNumber *n = objc_getAssociatedObject(self, @selector(tableViewSepStyle));
    return (UITableViewCellSeparatorStyle)n.integerValue;
}

@end

@interface MSPlacehodlerView ()

@property (nonatomic, strong) UIView    *contentView;
@property (nonatomic, strong) UIView    *placeholderImgView;
@property (nonatomic, strong) UILabel   *textLabel;

@end

@implementation MSPlacehodlerView

- (void)setText:(NSString *)text
{
    _text = text;
    self.textLabel.text = text;
}

- (void)setPlaceholderImg:(NSString *)placeholderImg
{
    _placeholderImg = placeholderImg;
    self.placeholderImgView.layer.contents = (id)MS_IMAGE_NAMED(placeholderImg).CGImage;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = MS_HEX_COLOR(0xEEEEEE);
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = UIColor.clearColor;
        
        [self addSubview:_contentView];
        
        [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.left.greaterThanOrEqualTo(self);
            make.center.equalTo(self);
        }];
        
        [_contentView addSubview:self.placeholderImgView];
        
        [self.placeholderImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(_contentView);
            make.left.greaterThanOrEqualTo(_contentView);
            make.width.mas_equalTo(572 * MS_ADAPTATION_RATIO);
            make.height.mas_equalTo(314 * MS_ADAPTATION_RATIO);
            make.centerX.equalTo(_contentView);
        }];
        
        [_contentView addSubview:self.textLabel];
        
        [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self.placeholderImgView.mas_bottom).offset(8 * MS_ADAPTATION_RATIO);
            make.left.greaterThanOrEqualTo(_contentView).offset(8 * MS_ADAPTATION_RATIO);
            make.bottom.lessThanOrEqualTo(_contentView).offset(-8 * MS_ADAPTATION_RATIO);
            make.centerX.equalTo(_contentView);
        }];
    }
    
    return self;
}


#pragma mark - lazy loading

- (UIView *)placeholderImgView
{
    if (!_placeholderImgView)
    {
        _placeholderImgView = [[UIView alloc] init];
    }
    
    return _placeholderImgView;
}

- (UILabel *)textLabel
{
    if (!_textLabel)
    {
        _textLabel = [[UILabel alloc] init];
        _textLabel.font = MS_SYSTEM_FONT_OF_SIZE(12);
        _textLabel.textColor = MS_HEX_COLOR(0x828282);
        _textLabel.textAlignment = NSTextAlignmentCenter;
        _textLabel.numberOfLines = 0;
    }
    
    return _textLabel;
}

@end
